package org.kjtc.weChatUtil;

import org.kjtc.redis.RedisUtil;

import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.UUID;

/**
 * Created By Donghua.Chen on  2018/1/9
 */
public class StringUtils {
    private static double EARTH_RADIUS = 6378.137;
    public static String UUID(){
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static boolean isEmpty(String str){
        return org.springframework.util.StringUtils.isEmpty(str);
    }

    public static String trimAllWhitespace(String str){
        return org.springframework.util.StringUtils.trimAllWhitespace(str);
    }
    public static String removeStart(String tstr, String rstr){
        return org.apache.commons.lang.StringUtils.removeStart(tstr,rstr);
    }

    public static LinkedList<String> stringArray2LinkedList(String[] split){

        LinkedList<String> stringLinkedList = new LinkedList<>();

        for (int i = 0; i <split.length ; i++) {

            if (!StringUtils.isEmpty(split[i])){

                stringLinkedList.add(split[i]);

            }

        }
        return stringLinkedList;

    };

    public static boolean contains(String dir, String src){

        return org.apache.commons.lang.StringUtils.contains(dir, src);

    }

    public static String replace(String str, String reql, String with){

         return org.springframework.util.StringUtils.replace(str, reql, with);
    }

    public static String getSqlClm(String sql) {
        String str ="";
        String[] spStr = sql.split(" ");
        for (int i = 0; i < spStr.length; i++) {
            if (i==3){
                str=spStr[3];
                break;
            }

        }
        return str;
    }

    public static String getAccessMassege(RedisUtil redisUtil, String appId, String str) {
        if(redisUtil.get(str)!=null){
        return   String.valueOf(redisUtil.get(str));
        }
        return appId;
    }
    public static String getEncodeCode(com.alibaba.fastjson.JSONObject json,String Str) {
        String re = null;
        try {
            re = new String(json.getString(Str).getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return re;
    }
    /**
     * 根据经纬度计算两点之间的距离的公式
     * @param longitude1
     * @param latitude1
     * @param longitude2
     * @param latitude2
     * @return
     */

    public static double algorithm(String longitude1, String latitude1, String longitude2, String latitude2) {

               double Lat1 = rad(Double.parseDouble(latitude1)); // 纬度

                double Lat2 = rad(Double.parseDouble(latitude2));

                double a = Lat1 - Lat2;//两点纬度之差

                double b = rad(Double.parseDouble(longitude1)) - rad(Double.parseDouble(longitude2)); //经度之差

                double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(Lat1) * Math.cos(Lat2) * Math.pow(Math.sin(b / 2), 2)));//计算两点距离的公式

                s = s * 6378137.0;//弧长乘地球半径（半径为米）

                s = Math.round(s * 10000d) / 10000d;//精确距离的数值

                 return s;

  }

  private static double rad(double d) {

        return d * Math.PI / 180.00; //角度转换成弧度

  }






        /**
         * 通过经纬度获取距离(单位：米)
         *
         * @param lat1
         * @param lng1
         * @param lat2
         * @param lng2
         * @return 距离
         */
        public static double getDistance(String lat1, String lng1, String lat2,String lng2) {
            double radLat1 = rad(Double.parseDouble(lat1));
            double radLat2 = rad(Double.parseDouble(lat2));
            double a = radLat1 - radLat2;
            double b = rad(Double.parseDouble(lng1)) - rad(Double.parseDouble(lng2));
            double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                    + Math.cos(radLat1) * Math.cos(radLat2)
                    * Math.pow(Math.sin(b / 2), 2)));
            s = s * EARTH_RADIUS;
            s = Math.round(s * 10000d) / 10000d;
            s = s * 1000;
            return s;
        }


/*
    public static void main(String[] args) {
        double distance = getDistance("116.368393","39.899052","116.310565","40.052499");
        System.out.println("距离" + distance / 1000 + "公里");
      double sdad =   algorithm("116.368393","39.899052","116.310565","40.052499");
        System.out.println((int)sdad);
    }
*/


}
